from selenium.webdriver.common.by import By

from base.mis.base_page import BasePage, BaseOperate
from utils import UtilsDriver


class LoginPage(BasePage):
    def __init__(self):
        super().__init__()
        self.username = By.CSS_SELECTOR, "[name='username']"
        self.password = By.CSS_SELECTOR, "[name='password']"
        self.login_btn = By.CSS_SELECTOR, '.input_sub'

    def find_username(self):
        return self.get_element(self.username)

    def find_password(self):
        return self.get_element(self.password)

    def find_login_btn(self):
        return self.get_element(self.login_btn)


class LoginOperate(BaseOperate):
    def __init__(self):
        self.lp = LoginPage()

    def input_username(self, username):
        self.input_text(self.lp.find_username(), username)

    def input_password(self, password):
        self.input_text(self.lp.find_password(), password)

    def click_login_btn(self):
        js = "document.getElementById('inp1').removeAttribute('disabled')"
        UtilsDriver.get_mis_driver().execute_script(js)
        self.lp.find_login_btn().click()


class LoginBusiness:
    def __init__(self):
        self.lo = LoginOperate()

    def go_login_to_home_page(self, username, password):
        self.lo.input_username(username)
        self.lo.input_password(password)
        self.lo.click_login_btn()
